package keye.document_manage.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import keye.document_manage.dao.sqlserver.IArchivenoFormatDao;
import keye.document_manage.domain.ArchivenoFormat;
import keye.document_manage.service.IArchivenoFormatService;

import org.springframework.stereotype.Service;

@Service("archivenoFormatService")
public class ArchivenoFormatServiceImpl implements IArchivenoFormatService{

	@Resource
	private IArchivenoFormatDao archivenoFormatDao;
	
	
	@Override
	public String[][] getSegmentRule(String entityId) {
		List<ArchivenoFormat> list = archivenoFormatDao.selectByEntityId(entityId);
		if(list == null || list.size()==0)
			return null;
		Collections.sort(list, new Comparator<ArchivenoFormat>(){

			@Override
			public int compare(ArchivenoFormat o1, ArchivenoFormat o2) {
				if(o1.getSegmentNo() > o2.getSegmentNo())
					return 1;
				else if(o1.getSegmentNo() == o2.getSegmentNo())
					return 0;
				else 
					return -1;
			}
			
		});
		int length = list.size();
		String[][] nameAndFileds = new String[3][];
		nameAndFileds[0] = new String[1];
		nameAndFileds[1] = new String[length];
		nameAndFileds[2] = new String[length];
		ArchivenoFormat af = null;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			af = list.get(i);
			nameAndFileds[1][i] = af.getSegmentChnName();
			nameAndFileds[2][i] = af.getSegmentName();
			nameAndFileds[0][0] = af.getSegmentSeparator();
			sb.append(nameAndFileds[1][i]+nameAndFileds[0][0]);
		}
		if(null != nameAndFileds[0][0] && !"".equals(nameAndFileds[0][0]))
			sb.deleteCharAt(sb.length()-1);
		nameAndFileds[0][0] = sb.toString();
		
		return nameAndFileds;
	}

}
